#e
#Title[uV̖Iv]
#Text[]
#Image[]
#BackGround[]
#BGM[]
#PlayLevel[Extra]
#ScriptVersion[2]
#Player[FREE]

script_enemy_main
{
let usebomb=0;
let phase=0;
let enemyx=GetCenterX;
let enemyy=0;
let movex=0;
let movey=0;

	//GoɈxs镔
	@Initialize
	{
	//   eϐ

	//   XyJ[hf[^
	SetLife(1750);
	SetScore(300000);
	SetDamageRate(20,  0);
	SetTimer(180);
	SetInvincibility(250);
	LoadGraphic("script\img\ExRumia.png");
	CutIn(KOUMA,"uV̖Iv",GetCurrentScriptDirectory~"..\img\cutin10.png",0,0,255,320);
	}
	//   ʒu܂ňړ
	SetMovePosition02(GetCenterX, GetCenterY-100, 60);

	BG_Standard;
	AtackTask;
	MoveTask;
	ControlTask;

	//G̃Ct0ɂȂ܂ŁA̕1t[1s
	@MainLoop
	{
		yield;

		if(OnBomb==false){
			SetCollisionA(GetX, GetY, 32);
			SetCollisionB(GetX, GetY, 24);
			usebomb=0;
		}
		else{
			usebomb++;
		}
		if(usebomb==1){
			BombBarrier;
		}
	}
	
	//`惋[vB1t[1s
	@DrawLoop
	{
		SetGraphicRect(64,1,127,64);
		if(phase>2){
			SetGraphicRect(0,1,63,64);
			if(movex<-1){
				SetGraphicRect(128,1,191,64);
			}
			if(movex>1){
				SetGraphicRect(192,1,255,64);
			}
		}
		SetColor(255,255,255);
		SetAlpha(255);
		if(OnBomb==true){SetAlpha(96);}
		SetTexture("script\img\ExRumia.png");//摜͔q
		DrawGraphic(GetX(),GetY());//G̍WEx[~A`;
	}

	//GłuԂɈxs镔
	@Finalize
	{
		DeleteGraphic("script\img\ExRumia.png");
		DeleteGraphic(GetCurrentScriptDirectory ~"..img\back1.png");
		DeleteGraphic(GetCurrentScriptDirectory ~"..img\back2.png");
	}
	//XyJ[h̔wi
	//Ă悢
	@BackGround
	{
		BG_Simple;
	}

	task AtackTask{
		
		wait(120);
		while(phase==0){	//̐΂T
			let nway=10;
			let dir=-60;
			let shift=rand(-3,3);
			loop(nway){
				CreateShot01(GetX,GetY,3.5,GetAngleToPlayer+dir+shift,BLUE11,0);
				dir+=90/nway;
			}
			wait(8);
			dir=-30;
			let shift=rand(-3,3);
			loop(nway){
				CreateShot01(GetX,GetY,3.5,GetAngleToPlayer+dir+shift,BLUE11,0);
				dir+=90/nway;
			}
			wait(8);

		}
		DeleteEnemyShotToItem(SHOT);
		wait(30);
		let dir=90;
		while(phase==1){	//̍d][^[
			let nway=12;
			let ammo=4;
			loop(ammo){
				loop(nway){
					CreateShot01(GetX,GetY,6,dir,BLUE02,0);
					CreateShot01(GetX,GetY,6,-dir,BLUE02,0);
					dir+=360/nway;
				}
				wait(2);
				dir+=1;
			}
			wait(8);
		}
		DeleteEnemyShotToItem(SHOT);
		wait(30);
		dir=0;
		while(phase==2){	//̔vA
			let nway=12;
			loop(nway){
				CreateShot01(GetX,GetY,4,dir+5,RED01,0);
				CreateShot01(cos(dir)*100+GetX,sin(dir)*100+GetY,4,dir-90,BLUE01,0);
				dir+=360/nway;
			}
			wait(10);
			dir-=4;
		}
		DeleteEnemyShotToItem(SHOT);
		wait(30);
		while(phase==3){	//΂̉ʒ[y􂷂ẽoT
			ExplodeShot(GetX,GetY,4,1,GetAngleToPlayer+rand(5,355),8,BLUE02,BLUE01,0);
			wait(5);
		}
		DeleteEnemyShotToItem(SHOT);
		wait(30);
		while(phase==4){	//΂180x{̃cCA
			let nway=24;
			CreateShot02(GetX-80,GetY,0,90,0.1,3,BLUE02,0);
			CreateShot02(GetX+80,GetY,0,90,0.1,3,BLUE02,0);
			wait(10);
			CreateShot02(GetX-80,GetY,0,90,0.1,3,BLUE02,0);
			CreateShot02(GetX+80,GetY,0,90,0.1,3,BLUE02,0);
			dir=0;
			loop(nway){
				CreateShot01(GetX,GetY,3,dir,RED01,0);
				dir+=180/nway;
			}
			wait(10);
			CreateShot02(GetX-80,GetY,0,90,0.1,3,BLUE02,0);
			CreateShot02(GetX+80,GetY,0,90,0.1,3,BLUE02,0);
			wait(10);
			CreateShot02(GetX-80,GetY,0,90,0.1,3,BLUE02,0);
			CreateShot02(GetX+80,GetY,0,90,0.1,3,BLUE02,0);
			dir=0;
			loop(nway){
				CreateShot01(GetX,GetY,2,dir,RED01,0);
				dir+=180/nway;
			}
			wait(10);
		}
		DeleteEnemyShotToItem(SHOT);
		wait(30);
		//΂̂ӂh
		dir=45;
		let spd=3;
		let rot=12;
		let open=-8;
		let ammo=32;
		let interval=5;
		let grf=PURPLE21;
		let delay=0;
		loop(16){
			RollingShotB(dir,spd,rot,open,ammo,interval,grf,delay);
			RollingShotB(dir+180,spd,rot,open,ammo,interval,grf,delay);
			dir+=5;
			open+=1;
		}

	}

	task RollingShotB(	//]CBɊJA]B
		dir,		//
		spd,		//e
		rot,		//]x
		open,		//WJEkx
		ammo,		//WJI܂ł̒e
		interval,	//ˌԊu
		grf,
		delay
	){
		while(true){
			loop(ammo){
				CreateShot01(GetX,GetY,spd,dir,grf,delay);
				wait(interval);
				dir+=rot+open;
			}
			loop(ammo){
				CreateShot01(GetX,GetY,spd,dir,grf,delay);
				wait(interval);
				dir+=rot-open;
			}
		}
	}

	task ExplodeShot(	//ʒ[Ŕe(jЂ̊px͌Œ)
		x,
		y,
		spda,
		spdb,
		dir,
		ammo,
		grfa,
		grfb,
		delay
	){
		let obj=Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj,x,y);
		Obj_SetSpeed(obj,spda);
		Obj_SetAngle(obj,dir);
		ObjShot_SetDelay(obj,delay);
		ObjShot_SetGraphic(obj,grfa);
		while(Obj_GetX(obj)>GetClipMinX&&Obj_GetX(obj)<GetClipMaxX&&
			Obj_GetY(obj)>GetClipMinY&&Obj_GetY(obj)<GetClipMaxY){
			wait(1);
		}
		dir=0;
		if(!Obj_BeDeleted(obj)){
			loop(ammo){
				CreateShot01(Obj_GetX(obj),Obj_GetY(obj),spdb,dir,grfb,delay);
				dir+=360/ammo;
			}
		}
	}

	task MoveSub(
		x,y,spd,pmax
	){
		let mode=0;
		let dir=rand(0,360);
		let rot=0;
		let change=rand_int(20,60);
		let cx=GetCenterX;
		let cy=GetCenterY/2;
		let shiftx=0;
		let shifty=0;
		let dist=(((cx-enemyx)*x)^2+((cy-enemyy)*y)^2)^0.5;	//Zoό`Bȉ~ŁB
		while(phase<pmax){
			enemyx=GetX;
			enemyy=GetY;
			movex=cos(dir)*spd;
			movey=sin(dir)*spd;
			let count=0;
			while(dist<160){
				shiftx=-(GetX-GetCenterX)*0.003;
				shifty=-(GetY-cy)*0.001;
				movex=cos(dir)*spd;
				movey=sin(dir)*spd;
				enemyx+=movex+shiftx;
				enemyy+=movey+shifty;
				SetX(enemyx);
				SetY(enemyy);
				dir+=rot;
				count++;
				if(count>=change){
					rot=rand(-3,3);
					count=0;
					change=rand_int(10,30);
				}
				wait(1);
				dist=(((cx-enemyx)*x)^2+((cy-enemyy)*y)^2)^0.5;
			}
			count=0;
			if(rot>0){rot=10;}else{rot=-10;}
			loop(18){
				shiftx=-(GetX-GetCenterX)*0.003;
				shifty=-(GetY-cy)*0.001;
				movex=cos(dir)*spd;
				movey=sin(dir)*spd;
				enemyx+=movex+shiftx;
				enemyy+=movey+shifty;
				SetX(enemyx);
				SetY(enemyy);
				count++;
				let fx=movex*10+enemyx;
				let fy=movey*10+enemyy;
				dist=(((cx-enemyx)*x)^2+((cy-enemyy)*y)^2)^0.5;
				if(dist<160){
					break;
				}
				if(count==18){break;}
				dir+=rot;
				wait(1);
			}
			count=0;
			change=10;
			rot=0;
			loop(10){
				shiftx=-(GetX-GetCenterX)*0.003;
				shifty=-(GetY-cy)*0.001;
				movex=cos(dir)*spd;
				movey=sin(dir)*spd;
				enemyx+=movex+shiftx;
				enemyy+=movey+shifty;
				SetX(enemyx);
				SetY(enemyy);
				wait(1);
			}
		}
	}
	task MoveTask{
		wait(120);
		MoveSub(8,12,1,4);
		while(phase<3){yield;}
		MoveSub(1,4,2,5);
		while(phase<5){yield;}
		MoveSub(1,12,1,6);
	}

	task ControlTask{
		let gate=250;
		wait(250);
		while(GetEnemyLife>gate*6){yield;}
		phase++;
		while(GetEnemyLife>gate*5){yield;}
		phase++;
		while(GetEnemyLife>gate*4){yield;}
		phase++;
		while(GetEnemyLife>gate*3){yield;}
		phase++;
		while(GetEnemyLife>gate*2){yield;}
		phase++;
	}

	#include_function".\function.txt"
}

